#include "ds.h"

#define SQL_CMD_creatTable "create table clients(name char primary key,password char)"
#define SQL_CMD_LENGTH		100

ds::ds()
{

}
ds::~ds()
{
	sqlite3_close(pDs);
}
int ds::open(string filepath)
{
	char *err;
	//没事写着玩，试试try catch 能不能用
	try
	{
		if(sqlite3_open(filepath.c_str(),&pDs) != SQLITE_OK)
		{
			errlog("sqlite3_open");
		}
	}
	catch(...)
	{
		errlog("catch a err");
	}

	cout << "create the databse successful!" << endl;

	if(sqlite3_exec(pDs,SQL_CMD_creatTable,NULL,NULL,&err) != SQLITE_OK)
	{
		printf("table is already exist\n");
	}
	cout << "create/open the table successful" << endl;
}
int ds::insert(string name,string password)
{
	char sql_cmd[SQL_CMD_LENGTH];
	char *err;

	sprintf(sql_cmd,"insert into clients values('%s','%s')",name.c_str(),password.c_str());

	if(sqlite3_exec(pDs,sql_cmd,NULL,NULL,&err) != SQLITE_OK)
	{
		printf("%s\n",err);
	}
}
int ds::find(string name,string password)
{
	char sql_cmd[SQL_CMD_LENGTH];
	char **result;
	char *err;
	int n_row,n_column;
	sprintf(sql_cmd,"select * from clients where name = '%s' and password = '%s'",name.c_str(),password.c_str());

	if(sqlite3_get_table(pDs,sql_cmd,&result,&n_row,&n_column,&err) != SQLITE_OK)
	{
		printf("get table : %s\n",err);
	}
	return n_row;

}
